﻿using JinJun.YShow.BLL;
using JinJun.YShow.BLL.IService;
using JinJun.YShow.Model;
using JinJun.YShow.Web.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace JinJun.YShow.Web.Controllers
{
    public class PictureController : BaseController
    {
        /*
         只定义了当前表的业务层，如需其他自行添加构造接口
            */

        private IPictureService _pictureService;

        public PictureController(IPictureService pictureService)
        {
            _pictureService = pictureService;
           
        }

        [HttpPost]
        public JsonResult UploadImage(HttpPostedFileBase file)
        {
            //先定义文件
            string fileName = Guid.NewGuid().ToString("N");
            //图片存放的路径
            string filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "Resource/Uploads");
            //如果没有图片
            if (Request.Files.Count == 0)
            {
                return Json(new { Success = false, FilePath = "" });
            }
            //获取图片的扩展名
            string stuf = Path.GetExtension(file.FileName);

            //图片存放路径不存在则创建
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            //保存图片
            string path = Path.Combine(filePath, fileName + stuf);
            file.SaveAs(path);

            //返回前端
            return Json(new { Success = true, FilePath = "/Resource/Uploads/" + fileName + stuf });
        }

        /// <summary>
        /// 获取图片列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetPicture(int page, int limit)
        {
            BaseService<Order_Detail> service = new BaseService<Order_Detail>();
            //定义表数据总数变量
            int count = 0;
            Expression<Func<Pic, bool>> lambdaCategory = a => true;
            Expression<Func<Pic, int>> orderCategory = a => a.Pic_Id;
            //创建List集合接收业务层返回值
            List<Pic> pics = _pictureService.QueryPageList(page, limit, lambdaCategory, orderCategory, out count);
            //创建PageListResult对象
            PageListResult<Pic> pageListResult = new PageListResult<Pic>();
            //框架属性赋值
            pageListResult.code = 0;
            pageListResult.count = count;
            pageListResult.msg = "";
            pageListResult.data = pics;
            //返回PageListResult对象
            return Json(pageListResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

       /// <summary>
       /// 修改是否主图
       /// </summary>
       /// <param name="id"></param>
       /// <param name="states"></param>
       /// <returns></returns>
        public JsonResult UpdateMaster(int id,short states)
        {
            Expression<Func<Pic, bool>> lambdaCategory = a => a.Pic_Id==id;
            Pic pic = _pictureService.Query(lambdaCategory);
            pic.Is_Master = states;
            pic.ModifiedTime = DateTime.Now;
            OperateResult operateResult = new OperateResult();
            operateResult.Success = _pictureService.Update(pic);
            return Json(operateResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 修改是否有效
        /// </summary>
        /// <param name="id"></param>
        /// <param name="states"></param>
        /// <returns></returns>
        public JsonResult UpdatePicState(int id, short states)
        {
            Expression<Func<Pic, bool>> lambdaCategory = a => a.Pic_Id == id;
            Pic pic = _pictureService.Query(lambdaCategory);
            pic.Pic_Status = states;
            pic.ModifiedTime = DateTime.Now;
            OperateResult operateResult = new OperateResult();
            operateResult.Success = _pictureService.Update(pic);
            return Json(operateResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="pic"></param>
        /// <returns></returns>
        public JsonResult AddPicture(Pic pic)
        {
            pic.ModifiedTime = DateTime.Now;
            OperateResult operateResult = new OperateResult();
            operateResult.Success = _pictureService.Add(pic);
            return Json(operateResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="pic"></param>
        /// <returns></returns>
        public JsonResult DeletePicture(Pic pic)
        {
            OperateResult operateResult = new OperateResult();
            operateResult.Success = _pictureService.Delete(pic);
            return Json(operateResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 编辑图片
        /// </summary>
        /// <param name="pic"></param>
        /// <returns></returns>
        public JsonResult EditPicture(Pic pic)
        {
            pic.ModifiedTime = DateTime.Now;
            OperateResult operateResult = new OperateResult();
            operateResult.Success = _pictureService.Update(pic);
            return Json(operateResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        ///// <summary>
        ///// 批量删除--实例
        ///// </summary>
        ///// <param name="pic"></param>
        ///// <returns></returns>
        //public JsonResult BatchDel(string id)
        //{
        //    //分割传上来的id
        //    string[] ids = id.Split(',');
        //    //定义个int数组
        //    int[] dis = new int[ids.Length];
        //    //将string数组复制到int数组
        //    for (int i = 0; i < ids.Length; i++)
        //    {
        //        dis[i] = Convert.ToInt32(ids[i]);
        //    }
        //    //定义List集合
        //    List<OrderInfo> orderInfos = new List<OrderInfo>();
        //    //循环获取对象
        //    for (int i = 0; i < dis.Length; i++)
        //    {
        //        int num = dis[i];
        //        //循环定义条件
        //        Expression<Func<OrderInfo, bool>> lambdaCategory = a => a.Order_Id ==num;
        //        //循环查出每一个对象存入list集合
        //        orderInfos.Add(_orderService.Query(lambdaCategory));
        //    }
        //    OperateResult operateResult = new OperateResult();
        //    //调用业务层批量删除方法把list集合放进去，  我这里我没写这个方法
        //    operateResult.Success = _orderService.BatchDel(orderInfos);
        //    return Json(operateResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        //}
    }
}